package com.xintonghua.base.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.xintonghua.base.BuildConfig;
import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.base.ui.MySubscriber;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * 作者：wanxianyi
 * 时间：2018/1/4.
 * 包名：com.xintonghua.printer.utils
 */

public class MyUtils {
    /**
     * 获得状态栏的高度
     */
    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 获取屏幕宽度
     */
    public static int getWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    /**
     * 获取屏幕高度
     */
    public static int getHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    /**
     * dip转换成px
     *
     * @return dip转换的px
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * toast提示
     *
     * @param msg
     */
    public static void mToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public static void openActivity(Context context, Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(context, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        context.startActivity(intent);
    }

    public static void openActivity(Context context, Class<?> pClass, Parcelable data) {
        Intent intent = new Intent(context, pClass);
        Bundle bundle = new Bundle();
        bundle.putParcelable(BuildConfig.FLAVOR, data);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    public static void openActivity(Context context, Class<?> pClass) {
        openActivity(context, pClass, new Bundle());
    }

    /**
     * 检查activity是否已经关闭
     *
     * @param mActivity 需要检查的activity
     * @return 结果
     */
    public static boolean activityIsFinished(Activity mActivity) {
        if (mActivity == null || mActivity.isFinishing()) {
            return true;
        }
        return false;
    }

    /**
     * 设置tabLayout
     *
     * @param context
     * @param tab
     * @param left
     * @param right
     */
    public static void setTabLine(Context context, TabLayout tab, int left, int right) {
        try {
            Class<?> tablayout = tab.getClass();
            Field tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab = (LinearLayout) tabStrip.get(tab);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                View child = ll_tab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                //修改两个tab的间距
                params.setMarginStart(MyUtils.dip2px(context, left));
                params.setMarginEnd(MyUtils.dip2px(context, right));
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断集合是否为null或者0个元素
     *
     * @param c
     * @return
     */
    public static boolean isNullOrEmpty(Collection c) {
        if (null == c || c.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * @param phoneNumber
     * @title 拨打电话
     */
    public static void callDial(Context context, String phoneNumber) {
        callDialTel(context, "tel:" + phoneNumber);
    }

    private static void callDialTel(Context context, String phoneProtocol) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(phoneProtocol));
        context.startActivity(intent);
    }

    public static File getDiskCacheDir(Activity mActivity, String path) {
        String basePath = getDiskCacheDir(mActivity);
        File file = new File(basePath, path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 获取缓存地址
     *
     * @return 系统的缓存地址
     */
    public static String getDiskCacheDir(Activity mActivity) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //获取data/android文件夹
            File dataFile = mActivity.getExternalCacheDir();
            if (dataFile == null) {
                dataFile = new File(Environment.getExternalStorageDirectory() + "/hongyi");
            }
            if (!dataFile.exists()) {
                //文件夹有可能不存在，需要创建
                dataFile.mkdirs();
            }
            return dataFile.getAbsolutePath();
        } else {
            return mActivity.getCacheDir().getAbsolutePath();
        }
    }
    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    public static void hideSoftInput(Activity activity) {
        View view = activity.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
